package com.gdut.huayan.controller;

import com.gdut.huayan.entity.dto.Result;
import com.gdut.huayan.entity.po.CharityPage;
import com.gdut.huayan.entity.po.Stock;
import com.gdut.huayan.service.CharityPageService;
import com.gdut.huayan.service.FileService;
import com.gdut.huayan.service.StockService;
import com.gdut.huayan.validation.group.GroupAdd;
import com.gdut.huayan.validation.group.GroupUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/suixi")
public class CharityPageController {

    @Autowired
    private FileService fileService;

    @Autowired
    private StockService stockService;

    @Autowired
    private CharityPageService charityPageService;

    // 返回所有随喜功德

    @PreAuthorize("permitAll()")
    @GetMapping("/getAll")
    public Result getAll(){
        //根据角色权限返回是否隐藏的随喜功德
        return Result.success(charityPageService.getAll());
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PutMapping("/add")
    public Result add(@RequestBody @Validated({Default.class, GroupAdd.class}) CharityPage charityPage, String requestId){
        charityPage.setCoverImageId(fileService.getFileStatus(requestId,true));
        charityPageService.save(charityPage);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/update")
    public Result update(@RequestBody @Validated({Default.class, GroupUpdate.class}) CharityPage charityPage){
        charityPageService.updateById(charityPage);
        return Result.success();
    }

    @Transactional
    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @DeleteMapping("/delete")
    public Result delete(Long charityPageId){
        charityPageService.removeById(charityPageId);
        return Result.success();
    }


}
